package com.sdp.wx.db.wx_ipc_menu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.wx.db.wx_ipc_menu.entity.WxIpcMenu;
import com.sdp.wx.db.wx_ipc_menu.mapper.WxIpcMenuMapper;
import com.sdp.wx.db.wx_ipc_menu.service.IWxIpcMenuService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-02-29
 */
@Service
public class WxIpcMenuServiceImpl extends ServiceImpl<WxIpcMenuMapper, WxIpcMenu> implements IWxIpcMenuService {

    @Override
    public List<JSONObject> queryIpcMenuTableTree() {
        List<WxIpcMenu> typeList = list();
        List<WxIpcMenu> rootList = new ArrayList<WxIpcMenu>();
        if(typeList!=null && typeList.size()>0) {
            for(WxIpcMenu type : typeList) {
                if(StringUtils.isEmpty(type.getParentId())) {
                    rootList.add(type);
                }
            }
        }
        List<JSONObject> treeList = new ArrayList<JSONObject>();
        if(rootList!=null && rootList.size()>0) {
            for(int i=0;i<rootList.size();i++) {
                JSONObject parentObj = JSON.parseObject(JSON.toJSONString(typeList.get(i), SerializerFeature.WriteMapNullValue).toString());
                treeList.add(parentObj);
                getChildIpcMenuTableTree(parentObj, typeList);
            }
        }
        return treeList;
    }
    private void getChildIpcMenuTableTree(JSONObject parentObj,List<WxIpcMenu> typeList) {
        List<Object> childList = new ArrayList<Object>();
        for(int j=0;j<typeList.size();j++) {
            if(!StringUtils.isEmpty(typeList.get(j).getParentId())&& parentObj.getString("keyId").equals(typeList.get(j).getParentId())) {
                if(parentObj.get("children")==null) {
                    parentObj.put("children", childList);
                }else {
                    childList = (List<Object>) parentObj.get("children");
                }
                JSONObject childObj = JSON.parseObject(JSON.toJSONString(typeList.get(j), SerializerFeature.WriteMapNullValue).toString());
                childList.add(childObj);
                getChildIpcMenuTableTree(childObj, typeList);
            }
        }
    }
    @Override
    public List<JSONObject> queryIpcMenuTreeSelect() {
        List<WxIpcMenu> typeList = list();
        List<WxIpcMenu> rootList = new ArrayList<WxIpcMenu>();
        if(typeList!=null && typeList.size()>0) {
            for(WxIpcMenu type : typeList) {
                if(StringUtils.isEmpty(type.getParentId())) {
                    rootList.add(type);
                }
            }
        }
        List<JSONObject> treeList = new ArrayList<JSONObject>();
        if(rootList!=null && rootList.size()>0) {
            for(int i=0;i<rootList.size();i++) {
                JSONObject parentObj = new JSONObject();
                parentObj.put("title", rootList.get(i).getMenuTitle());
                parentObj.put("value", rootList.get(i).getKeyId());
                parentObj.put("key",rootList.get(i).getKeyId());
                treeList.add(parentObj);
                getChildIpcMenuTreeSelect(parentObj, typeList);
            }
        }
        return treeList;
    }
    private void getChildIpcMenuTreeSelect(JSONObject parentObj,List<WxIpcMenu> typeList) {
        List<Object> childList = new ArrayList<Object>();
        for(int j=0;j<typeList.size();j++) {
            if(!StringUtils.isEmpty(typeList.get(j).getParentId())&& parentObj.getString("key").equals(typeList.get(j).getParentId())) {
                if(parentObj.get("children")==null) {
                    parentObj.put("children", childList);
                }else {
                    childList = (List<Object>) parentObj.get("children");
                }
                JSONObject childObj = new JSONObject();
                childObj.put("title", typeList.get(j).getMenuTitle());
                childObj.put("value", typeList.get(j).getKeyId());
                childObj.put("key",typeList.get(j).getKeyId());
                childList.add(childObj);
                getChildIpcMenuTreeSelect(childObj, typeList);
            }
        }
    }
}
